﻿using System.Data;
using System.Reflection;
using DTO;
using System;
using System.Collections.Generic;

namespace DAO
{
    public class SanPham_Dao
    {
        private readonly BaseOperationImplement<SanPham_Dto> _baseOperationImp = new BaseOperationImplement<SanPham_Dto>();


        public DataTable GetAllObject()
        {
            return _baseOperationImp.GetAllObject("SanPham");
        }

        public SanPham_Dto GetSanPham(string masp)
        {
            SanPham_Dto sanpham = new SanPham_Dto();
            DataTable dt =  _baseOperationImp.GetAllObject("SanPham WHERE MaSP='" + masp + "'");
            PropertyInfo[] proInfo = sanpham.GetType().GetProperties();

            foreach (DataRow dr in dt.Rows)
            {
                for (int i = 0; i < proInfo.Length; i++)
                {
                    proInfo[i].SetValue(sanpham, Convert.ChangeType(dr.ItemArray[i].ToString(), proInfo[i].PropertyType), null);
                }
            }
            return sanpham;
        }

        public DataSet GetSanPhams(string  makh, string manv)
        {
            IList<SanPham_Dto> lstSanPham = new List<SanPham_Dto>();

            DataSet ds = _baseOperationImp.GetDataSet(@"select sp.MaSP, sp.TenSP, sp.MaLoai, sp.MaNuoc, sp.MaChatLieu
                                                            , sp.GiaNhap, sp.GiaBan, sp.SoLuong, sp.MaMau, sp.HinhAnh
                                                            from dbo.ChiTietHDB cthdb 
                                                            join dbo.HoaDonBan hdb
                                                            on cthdb.MaHDB = hdb.MaHDB
                                                            join dbo.SanPham sp
                                                            on cthdb.MaSP = sp.MaSP
                                                            where MaKH = '" + makh + "' and MaNV = '" +  manv +"'");
            return ds;
        }

        public int AddObject(SanPham_Dto obj)
        {
            return _baseOperationImp.AddObject("insSanPham", CommandType.StoredProcedure, "@masp", obj.MaSP, "@tensp",
                                               obj.TenSP, "@maloai", obj.MaLoai, "@manuoc", obj.MaNuoc,
                                               "@macl", obj.MaChatLieu, "@gianhap", obj.GiaNhap, "@giaban", obj.GiaBan,
                                               "@soluong", obj.SoLuong, "@mamau", obj.MaMau, "@hinhanh", obj.HinhAnh);
        }

        public int EditObject(string id, SanPham_Dto newObj)
        {
            return _baseOperationImp.AddObject("updateSanPham", CommandType.StoredProcedure, "@masp", id, "@tensp",
                                               newObj.TenSP,
                                               "@maloai", newObj.MaLoai, "@manuoc", newObj.MaNuoc, "@macl",
                                               newObj.MaChatLieu, "@mamau", newObj.MaMau, "@hinhanh", newObj.HinhAnh);
        }

        public int DeleteObject(string id)
        {
            return _baseOperationImp.DeleteObject("deleteSanPham", CommandType.StoredProcedure, "@id", id);
        }


        public int AutoIncrementIdObject()
        {
            return _baseOperationImp.AutoIncrementIdObject("SanPham", "MaSP");
        }

        public DataTable FindObject(string condition)
        {
            var helper = new Helper();
            return helper.GetDataTable("SELECT * FROM SanPham WHERE "+ condition);
        }
    }
}